<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="调解中心" name="configId">
						<a-select v-model:value="searchFormState.configId" placeholder="请选择所属调解中心">
							<a-select-option v-for="item in configArr" :key="item.id" :value="item.id"
								>{{ item.name }}
							</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="欠款人姓名" name="debtorName">
						<a-input
							v-model:value.trim="searchFormState.debtorName"
							placeholder="请输入欠款人姓名"
							allowClear
							@pressEnter="tableRef.refresh(true)"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="欠款人身份证号" name="debtorCardId">
						<a-input
							v-model:value.trim="searchFormState.debtorCardId"
							placeholder="请输入欠款人身份证号"
							allowClear
							@pressEnter="tableRef.refresh(true)"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="调解员姓名" name="mediatorName">
						<a-input
							v-model:value.trim="searchFormState.mediatorName"
							placeholder="请输入调解员姓名"
							allowClear
							@pressEnter="tableRef.refresh(true)"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="上传日期">
						<a-range-picker v-model:value="scopeDateArr" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen(null)">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
					<a-button type="primary" @click="uploadRef.onOpen()">
						<UploadOutlined />
						导入
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'officialSealUrl' && record.officialSealUrl">
					<a-image
						:width="40"
						:src="record.officialSealUrl"
						:preview="{
							src: record.officialSealUrl
						}"
					/>
				</template>
				<template v-else-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" primary size="small" @click="formRef.onOpen(record)">编辑</a-button>
						<a-button type="link" primary size="small" @click="previewRef.onOpen(record)">预览</a-button>
						<a-popconfirm title="确定要删除此公司吗？" @confirm="deleteModule(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh(true)" />
	<Upload ref="uploadRef" @successful="tableRef.refresh(true)" />
	<Preview ref="previewRef" />
</template>

<script setup name="Company">
	import { Image as AImage } from 'ant-design-vue'
	import Form from '@/views/website/protocol/form.vue'
	import mediationProtocolApi from '@/api/website/protocol/mediationProtocolApi'
	import Upload from '@/views/website/protocol/upload.vue'
	import Preview from '@/views/website/protocol/preview.vue'
	import configApi from '@/api/website/config/configApi'
	import dayjs from 'dayjs'

	const searchFormState = ref({
		createUser: undefined,
		deptId: undefined
	})
	const tableRef = ref()
	const formRef = ref()
	const previewRef = ref()
	const uploadRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const configArr = ref()
	const scopeDateArr = ref([dayjs().subtract(31, 'day'), dayjs().endOf('day')])
	const columns = [
		{
			title: '调解中心名称',
			dataIndex: 'companyName',
			align: 'center'
		},
		{
			title: '省份简称',
			dataIndex: 'provAbbr',
			align: 'center'
		},
		{
			title: '欠款人姓名',
			dataIndex: 'debtorName',
			align: 'center'
		},
		{
			title: '欠款人身份证号',
			dataIndex: 'debtorCardId',
			align: 'center'
		},
		{
			title: '调解状态',
			dataIndex: 'mediationStatusName',
			align: 'center'
		},
		{
			title: '调解函号',
			dataIndex: 'mediationNum',
			align: 'center'
		},
		{
			title: '调解编号号',
			dataIndex: 'documentNumber',
			align: 'center'
		},
		{
			title: '平台',
			dataIndex: 'platform',
			align: 'center'
		},
		{
			title: '产品编码',
			dataIndex: 'product',
			align: 'center'
		},
		{
			title: '借款金额',
			dataIndex: 'loanAmount',
			align: 'center',
			render: (value) => `¥${value.toFixed(2)}`
		},
		{
			title: '案件日期',
			dataIndex: 'caseDate',
			align: 'center'
		},
		{
			title: '调解中心官网',
			dataIndex: 'officialWebsite',
			ellipsis: true,
			align: 'center'
		},
		{
			title: '公章',
			dataIndex: 'officialSealUrl',
			align: 'center',
			width: 100
		},
		{
			title: '微信公众号',
			dataIndex: 'weChatOfficialAccount',
			align: 'center'
		},
		{
			title: '调解员',
			dataIndex: 'mediatorName',
			align: 'center'
		},
		{
			title: '调解员电话',
			dataIndex: 'mediationPersonPhone',
			align: 'center'
		},
		{
			title: '调解中心电话',
			dataIndex: 'mediationCenterPhone',
			align: 'center'
		},
		{
			title: '监督举报电话',
			dataIndex: 'supervisionPhone',
			align: 'center'
		},
		{
			title: '调解协议出具日期',
			dataIndex: 'mediationLetterDate',
			align: 'center'
		},
		{
			title: '诉讼状态',
			dataIndex: 'litigationStatusName',
			align: 'center'
		},
		{
			title: '有效期',
			dataIndex: 'expiryDate',
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right',
			width: 100
		}
	]

	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		searchFormState.value.beginTime = scopeDateArr.value[0].format('YYYY-MM-DD').concat(' 00:00:00')
		searchFormState.value.endTime = scopeDateArr.value[1].format('YYYY-MM-DD').concat(' 23:59:59')
		return mediationProtocolApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}

	const deleteModule = (record) => {
		mediationProtocolApi.del(record.id).then((res) => {
			tableRef.value.refresh(true)
		})
	}

	onMounted(() => {
		configApi.getCompanyDict().then((res) => {
			configArr.value = Object.entries(res).map(([id, name]) => ({
				id: parseInt(id),
				name: name
			}))
		})
	})
</script>
